/////////////////////////////////////////////////////////////////////////////
// Original code from libhdfs3. Copyright (c) 2013 - 2014, Pivotal Inc.
// All rights reserved. Author: Zhanwei Wang
/////////////////////////////////////////////////////////////////////////////
//  Modifications by Kumo Inc.
// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//



#ifndef _HDFS_LIBHDFS3_CLIENT_LEASE_RENEW_H_
#define _HDFS_LIBHDFS3_CLIENT_LEASE_RENEW_H_

#include <map>

#include <atomic>
#include <memory>
#include <kmhdfs/common/thread.h>

namespace Hdfs {
namespace Internal {

class FileSystemInter;

class LeaseRenewer {
public:
    virtual ~LeaseRenewer() {
    }

    virtual void StartRenew(std::shared_ptr<FileSystemInter> filesystem) = 0;
    virtual void StopRenew(std::shared_ptr<FileSystemInter> filesystem) = 0;

public:
    static LeaseRenewer & GetLeaseRenewer();
    static void CreateSinglten();

private:
    static once_flag once;
    static std::shared_ptr<LeaseRenewer> renewer;
};

class LeaseRenewerImpl: public LeaseRenewer {
public:
    LeaseRenewerImpl();
    ~LeaseRenewerImpl();
    int getInterval() const;
    void setInterval(int interval);
    void StartRenew(std::shared_ptr<FileSystemInter> filesystem);
    void StopRenew(std::shared_ptr<FileSystemInter> filesystem);

private:
    void renewer();

private:
    std::atomic<bool> stop;
    condition_variable cond;
    int interval;
    mutex mut;
    std::map<std::string, std::shared_ptr<FileSystemInter> > maps;
    thread worker;
};

}
}
#endif /* _HDFS_LIBHDFS3_CLIENT_LEASE_RENEW_H_ */
